$(function () {
    //使用事例
    //<div class="ybUeditor" name="test2" value="ddddddd22222"></div>
    ybUEditorInit();
});

function ybUEditorInit(pNode){
    ((undefined === pNode) || ('' === pNode))
        ? pNode = $('body')
        : (
        (pNode instanceof jQuery)
            ? false
            : pNode = $(pNode)
    );

    pNode.find('.ybUEditor').each(function () {
        ybCreateUEditor($(this));
    });

}

function ybCreateUEditor(viewContainer) {
    var content = new Array();

    var viewId = getRandStr() + (new Date().getTime());
    var name = viewContainer.data('name');
    var value = viewContainer.data('value');

    content.push(
        '<div id="' + viewId + '"> ' +
        '   <script id="' + viewId + '_editor" type="text/plain"' +
            ((name != undefined && name != '') ? 'name="' + name + '" ' : '') +
        '   >' + ((value != undefined && value != '') ? value : '') + '</script> ' +
        '</div>'
    );
    viewContainer.html(content.join(''));

    //加载Ueditor
    var editorConfig = viewContainer.attr('ueditor-config');
    if (editorConfig != undefined && editorConfig != '') {
        editorConfig = eval('(' + editorConfig + ')');
        UE.getEditor(viewId + '_editor', editorConfig);
    } else {
        UE.getEditor(viewId + '_editor',{
            initialFrameHeight: 250,
            autoHeightEnabled: false
        });
    }
}

function getRandStr(lenLimit) {
    lenLimit = lenLimit || 8;
    var srcStr = 'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
    var maxPos = srcStr.length;
    var desStr = '';
    for (var i = 0; i < lenLimit; i++) {
        desStr += srcStr.charAt(Math.floor(Math.random() * maxPos));
    }
    return desStr;
}